import { Effect, Reducer } from 'umi';

import {
  saveCreateGroup,
  saveRenameGroup,
  removeGroup,
  addWidgets,
  deleteWidgets,
  updateGroup,
  setGrobalFilter,
  queryDashboardOperation, addFolder,
} from '@/services/analysis';
import { IDashboardOperation } from '@/pages/data';
import { getDashboard } from '@/services/selfquery';
import { IDashboardItem } from '@/pages/SelfQuery/type';

export interface BoardModelState {
  dashboardOperations: IDashboardOperation[]
  dashboard: IDashboardItem | any
}

export interface BoardModelType {
  namespace: 'board';
  state: BoardModelState;
  effects: {
    fetchDashboardOperation: Effect;
    fetchDashboard: Effect;
    addFolder: Effect;
    saveCreateGroup: Effect;
    saveRenameGroup: Effect;
    deletGroup: Effect;
    saveAddWidgets: Effect;
    updateGroup: Effect;
    deleteWidgets: Effect;
    setGrobalFilter: Effect;
    clearBoard: Effect;
  };
  reducers: {
    save: Reducer;
  };
}

const BoardModel: BoardModelType = {
  namespace: 'board',

  state: {
    dashboardOperations: [],
    dashboard: undefined,
  },

  effects: {
    // 获取看板数据及操作行为日志
    * fetchDashboardOperation({ payload, callback }: any, { call, put }: any) {
      const response = yield call(queryDashboardOperation, payload);
      yield put({
        type: 'save',
        payload: { dashboardOperations: response.payload },
      });
    },
    // 获取指标列表
    * fetchDashboard({ payload, callback }: any, { call, put }: any) {
      const response = yield call(getDashboard, payload);
      yield put({
        type: 'save',
        payload: { dashboard: response.payload },
      });
      if (callback) callback(response.payload);
    },
    // 创建分组
    * addFolder({ payload, callback }: any, { call }: any) {
      const response = yield call(addFolder, payload);

      if (callback) callback(response);
    },

    // 创建分组
    * saveCreateGroup({ payload, callback }: any, { call }: any) {
      const response = yield call(saveCreateGroup, payload);

      if (callback) callback(response);
    },

    // 创建分组重命名
    * saveRenameGroup({ payload, callback }: any, { call }: any) {
      const response = yield call(saveRenameGroup, payload);

      if (callback) callback(response);
    },

    // 删除分组
    * deletGroup({ payload, callback }: any, { call }: any) {
      const response = yield call(removeGroup, payload);

      if (callback) callback(response);
    },

    // 看板添加指标
    * saveAddWidgets({ payload, callback }: any, { call }: any) {
      const response = yield call(addWidgets, payload);

      if (callback) callback(response);
    },

    // 看板删除指标
    * deleteWidgets({ payload, callback }: any, { call }: any) {
      const response = yield call(deleteWidgets, payload);

      if (callback) callback(response);
    },


    // 更新分组指标
    * updateGroup({ payload, callback }: any, { call }: any) {
      const response = yield call(updateGroup, payload);

      if (callback) callback(response);
    },

    // 更新分组指标
    * setGrobalFilter({ payload, callback }: any, { call }: any) {
      const response = yield call(setGrobalFilter, payload);

      if (callback) callback(response);
    },
    * clearBoard(_, { put }: any) {
      yield put({
          type: 'save',
          payload: { dashboard: undefined },
        },
      );
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};

export default BoardModel;
